package com.bana.fvs.protocol;

import com.bana.fvs.code.DeviceStatus;
import com.bana.fvs.model.Controller;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

/**
 * 查询控制器基本信息
 * 开始地址，结束地址，巡检间隔时间
 */
public class QueryController {

    public int getId()
    {
        return 0x11;
    }

    public byte[] toBuffer(int kzqbsm)
    {
        byte[] buffer=new byte[4];
        buffer[0]=(byte)0x81;
        buffer[1]=(byte)kzqbsm;
        buffer[2]=(byte)getId();
        buffer[3]=(byte)0xff;
        return buffer;
    }

    public Controller toModel(byte[] buffer) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
        DataInputStream dis = new DataInputStream(bais);
        dis.skipBytes(1);
        Controller controller=new Controller();
        controller.setKzqbsm(dis.readByte());
        dis.skipBytes(1);
        short s1=dis.readByte();
        short s2=dis.readByte();
        controller.setKsdz(s2*128+s1);
        s1=dis.readByte();
        s2=dis.readByte();
        controller.setJsdz(s2*128+s1);
        s1=dis.readByte();
        s2=dis.readByte();
        controller.setKsdz1(s2*128+s1);
        s1=dis.readByte();
        s2=dis.readByte();
        controller.setJsdz1(s2*128+s1);
        s1=dis.readByte();
        s2=dis.readByte();
        controller.setKsdz2(s2*128+s1);
        s1=dis.readByte();
        s2=dis.readByte();
        controller.setJsdz2(s2*128+s1);
        s1=dis.readByte();
        s2=dis.readByte();
        controller.setKsdz3(s2*128+s1);
        s1=dis.readByte();
        s2=dis.readByte();
        controller.setJsdz3(s2*128+s1);
        controller.setWdxjjg(dis.readByte());
        controller.setSbzt(DeviceStatus.Noraml);
        dis.close();
        bais.close();
        return controller;
    }
}
